package solicitudes.dao.impl;

import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import solicitudes.dao.DAOPersona;
import solicitudes.entity.Persona;

/***
 * 
 * @author MARIA ALEJANDRA CELEMIN R.
 * @version 1.0
 * 
 * Clase que implementa los metodos de la interfaz DAOPersona
 * @see DAOPersona
 *
 */
public class DAOPersonaImpl extends HibernateDaoSupport implements DAOPersona {

 private static Logger logger = Logger.getLogger(DAOPersonaImpl.class);
 
 /* (non-Javadoc)
  * @see solicitudes.dao.DAOPersona#obtenerPersonas()
  */
 @SuppressWarnings("unchecked")
 @Override
 public List<Persona> obtenerPersonas() {
  
  Session session = getSession();
  List<Persona> personas = new ArrayList<Persona>();
  
  try {
   personas = session.createQuery("from Persona").list();
  } catch (HibernateException ex) {
   logger.debug("Exception encontrada" + ex.getLocalizedMessage());
  } finally {
   session.close();
  }
  
  return personas;
 }

 /* (non-Javadoc)
  * @see solicitudes.dao.DAOPersona#obtenerPersona()
  */
 @Override
 public Persona obtenerPersona(String numeroDoc) {
 
  Session session = getSession();
  Persona persona = null;
  
  try {
   persona = (Persona) session.get(Persona.class, numeroDoc);
  } catch (HibernateException ex) {
   logger.debug("Exception encontrada" + ex.getLocalizedMessage());
  } finally {
   session.close();
  }
  
  return persona;
 }

 /* (non-Javadoc)
  * @see solicitudes.dao.DAOPersona#guardarPersona(solicitudes.entity.Persona)
  */
 @Override
 public void guardarPersona(Persona persona) {
  
  Session session = getSession();
  session.beginTransaction();
  
  try {
   session.save(persona);
   session.getTransaction().commit();
   
  } catch (HibernateException ex) {
   logger.debug("Exception encontrada" + ex.getLocalizedMessage());
  } finally {
   session.close();
  }  
 }

 /* (non-Javadoc)
  * @see solicitudes.dao.DAOPersona#eliminarPersona(solicitudes.entity.Persona)
  */
 @Override
 public void eliminarPersona(Persona persona) {
  
  Session session = getSession(); 
  session.beginTransaction();
  
  try {
   session.delete(persona);
   session.getTransaction().commit();
  } catch (HibernateException ex) {
   logger.debug("Exception encontrada" + ex.getLocalizedMessage());
  } finally {
   session.close();
  }
  
 }

 /* (non-Javadoc)
  * @see solicitudes.dao.DAOPersona#editarPersona(solicitudes.entity.Persona)
  */
 @Override
 public void editarPersona(Persona persona) {
  
  Session session = getSession(); 
  session.beginTransaction();
  
  try {
   session.merge(persona);
   session.getTransaction().commit();
  } catch (HibernateException ex) {
   logger.debug("Exception encontrada" + ex.getLocalizedMessage());
  } finally {
   session.close();
  }
  
 }

}